using System;
using System.IO;
using System.Web;

namespace SoftwareMonkeys.Scriptech.Scripts
{
	public class ScriptReader
	{
		static public string[] GetScripts()
		{
			string scriptsDirectory = HttpContext.Current.Server.MapPath (HttpContext.Current.Request.ApplicationPath + "/Scripts");
			
			string[] scriptPaths = Directory.GetFiles (scriptsDirectory);
			
			return scriptPaths;
		}
		
		static public string GetContent(string scriptName)
		{
			string content = String.Empty;
			
			string scriptPath = ScriptFileNamer.GetPath(scriptName);
			
			using (StreamReader reader = new StreamReader(File.OpenRead(scriptPath)))
			{
				content = reader.ReadToEnd();
			}
			
			return content;
		}
		
		static public string GetTemplate(string scriptType)
		{
			if (scriptType == null)
				throw new ArgumentNullException("scriptType");
			string templateName = String.Empty;
			
			if (scriptType.Trim('.') == "ascx")
			{
				templateName = "DefaultScript.ascxt";
			}
			else
			{
				templateName = "DefaultScriptlet.scriptt";
			}
			
			string path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/ScriptTemplates/" + templateName);
			
			string content = String.Empty;
			
			using (StreamReader reader = new StreamReader(System.IO.File.OpenRead(path)))
			{
				content = reader.ReadToEnd();
			}
			
			return content;
		}
	}
}

